package com.example.zyfyptlzx729.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.activities.ViewArticleActivity;
import com.example.zyfyptlzx729.bean.ArticleBean;
import com.example.zyfyptlzx729.common.Common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ArticleAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<ArticleBean> list;

    public ArticleAdapter(Context context){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }



    public void setList(List<ArticleBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ArticleBean bean = list.get(position);
        if(bean == null)
            return;
        ViewHolder viewHolder = (ViewHolder)holder;
        Picasso.with(context)
                .load(Common.IMAGEURL + bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);

        viewHolder.tvtitle.setText(bean.getName());
        viewHolder.tvauthor.setText(bean.getAuthor());
        viewHolder.tvtime.setText(bean.getUpdate_time());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = bean.getId();
                Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context, ViewArticleActivity.class);
                intent.putExtra("resid",bean.getId());
                intent.putExtra("authorid",bean.getUserid());
                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        if(list != null)
            return list.size();
        else return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvtitle, tvauthor, tvtime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView2);
            tvtitle = itemView.findViewById(R.id.textView4);
            tvauthor = itemView.findViewById(R.id.textView6);
            tvtime = itemView.findViewById(R.id.textView10);

        }
    }
}
